package addServlet;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import pojo.User;
import dao.impl.UserDaoImpl;

/**
 * Servlet implementation class UploadServlet
 */
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


		System.out.println(".......................");
		DiskFileItemFactory factory=new DiskFileItemFactory(20*1024, new File("/temp1"));
		ServletFileUpload upload=new ServletFileUpload(factory);
		upload.setSizeMax(80*1024);
		upload.setHeaderEncoding("UTF-8");
		
		String name=null;
		boolean sex = false;
		String birth=null;
		double sal = 0;
		byte[] bytes = null;
		
		try {
			List<FileItem> items= upload.parseRequest(request);
			for(FileItem item:items){
				if(item.isFormField()){
					if("username".equals(item.getFieldName())){
						System.out.println(item.getString("UTF-8"));
						request.setAttribute("username", item.getString("UTF-8"));
					} else if ("salary".equals(item.getFieldName())){
						System.out.println(item.getString("UTF-8"));
						request.setAttribute("salary", item.getString("UTF-8"));
					} else if ("birth".equals(item.getFieldName())){
						System.out.println(item.getString("UTF-8"));
						request.setAttribute("birth", item.getString("UTF-8"));
					} else if ("sex".equals(item.getFieldName())){
						System.out.println(item.getString("UTF-8"));
						request.setAttribute("sex", item.getString("UTF-8"));
					}
				}
				else{
					System.out.println(item.getName()+"...."+item.getSize());
					BufferedInputStream in = new BufferedInputStream(item.getInputStream());
					ByteArrayOutputStream out = new ByteArrayOutputStream();
					byte[] buf = new byte[2048];
					int len = 0;
					while((len = in.read(buf))!=-1){
					    out.write(buf,0,len);
					}
					bytes = out.toByteArray();
					System.out.println("img bytes len   " + bytes.length);
					out.close();
					
				}
			
			}
//			if (name!=null) {
				User user=new User();
				user.setName(name);
				user.setBirth(birth);
				user.setSal(sal);
				user.setSex(sex);
				user.setPortait(bytes);
				UserDaoImpl udi=new UserDaoImpl();
				try {
					udi.addUser(user);
				} catch (SQLException e) {
					response.sendRedirect("ErrorServlet");
				}
				PrintWriter writer=response.getWriter();
				writer.write("success...");
				response.sendRedirect("ShowAllServlet");
//			}else {
//				response.sendRedirect("ErrorServlet");
//			}
			
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
			
	
	
	}

}
